home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / prcgntn1.lha / Precognition / source / OutlineBox.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  6KB  |  240 lines

  1.  
  2. #include "OutlineBox.h"
  3. #include "GraphicObjectClass.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "minmax.h"
  7. #include <graphics/gfxmacros.h>
  8. #include <proto/exec.h>
  9. #include <proto/intuition.h>
  10. #include <proto/graphics.h>
  11. #include "amigamem.h"
  12.  
  13.  
  14. tPoint OutlineBox_Location( OutlineBox *self )
  15. {
  16.    return self->Location;
  17. }
  18.  
  19.  
  20. tPoint OutlineBox_SetLocation( OutlineBox *self,
  21.                              PIXELS    LeftEdge,
  22.                              PIXELS    TopEdge )
  23. {
  24.    self->Location.x = LeftEdge;
  25.    self->Location.y = TopEdge;
  26.    return self->Location;
  27. }
  28.  
  29. tPoint OutlineBox_Size( OutlineBox *self )
  30. {
  31.    return self->Size;
  32. }
  33.  
  34. tPoint OutlineBox_AskSize( OutlineBox      *self,
  35.                          PIXELS         Width,
  36.                          PIXELS         Height )
  37. {
  38.    tPoint size;
  39.  
  40.    size.x = MAX( Width, 6 );
  41.    size.y = MAX( Height, 6 );
  42.    return size;
  43. }
  44.  
  45.  
  46. tPoint OutlineBox_SetSize( OutlineBox *self,
  47.                          PIXELS    Width,
  48.                          PIXELS    Height )
  49. {
  50.    tPoint size;
  51.  
  52.    size = AskSize( self, Width, Height );
  53.    self->Size.x = size.x;
  54.    self->Size.y = size.y;
  55.  
  56.    pcg_Init3DThinBevel( &self->Bevel, 0,0, size.x, size.y, 0,
  57.       self->Pens.DarkPen, self->Pens.BrightPen, NULL );
  58.  
  59.    return size;
  60. }
  61.  
  62.  
  63. #define FONT_WIDTH  8
  64. #define FONT_HEIGHT 8
  65. #define BASELINE    7
  66.  
  67. void OutlineBox_Render( OutlineBox *self,
  68.                       RastPort *RPort )
  69. {
  70.    short width, length, x, y;
  71.    Point location;
  72.    Point size;
  73.  
  74.    location = Location(self);
  75.    size     = Size(self);
  76.    length   = strlen(self->Label);
  77.    width    = length * FONT_WIDTH;
  78.    x        = location.x + size.x/2 - width/2;
  79.    y        = location.y - FONT_HEIGHT/2 + BASELINE;
  80.  
  81.    DrawBorder( RPort, (struct Border*) &self->Bevel, location.x, location.y );
  82.  
  83.    SetDrMd( RPort, JAM2 );
  84.    SetAPen( RPort, self->Pens.FrontPen );
  85.    SetBPen( RPort, self->Pens.BackPen );
  86.    Move( RPort, x, y );
  87.    Text( RPort, self->Label, length );
  88. }
  89.  
  90. char *OutlineBox_Title( OutlineBox *self )
  91. {
  92.    return self->Label;
  93. }
  94.  
  95. BOOL OutlineBox_SetTitle( OutlineBox *self, char *title )
  96. {
  97.    Afree( self->Label );
  98.    self->Label = Astrdup(title);
  99.    return TRUE;
  100. }
  101.  
  102.  
  103. #ifdef BUILDER
  104. #include "BuilderMethods.h"
  105. #include "GraphicObject_Builder.h"
  106. #include "OutlineBox_coder.h"
  107.  
  108. OutlineBox *OutlineBox_New( OutlineBox *self )
  109. {
  110.    OutlineBox *new_box = NULL;
  111.  
  112.    if (new_box = Amalloc(sizeof(OutlineBox)))
  113.    {
  114.  
  115.       OutlineBox_Init( new_box, self->Location.x, self->Location.y,
  116.          self->Size.x, self->Size.y, self->Pens, Title(self) );
  117.  
  118.       GiveItAName(new_box);
  119.  
  120.    }
  121.  
  122.    return new_box;
  123. }
  124.  
  125. #define YSHIFT 5
  126.  
  127. BOOL OutlineBox_InitImageBob( OutlineBox  *self,
  128.                               ImageBob    *ibob )
  129. {
  130.    Point     size, location;
  131.    UWORD     width, length, x, y;
  132.  
  133.    size     = Size(self);
  134.    y        = size.y + YSHIFT;
  135.    location = Location(self);
  136.  
  137.    /* Allocate memory for image. */
  138.    if (ImageBob_Init( ibob, size.x, y, 2 ))
  139.    {
  140.  
  141.       SetLocation( self, 0, 5 );
  142.  
  143.       Render( self, ibob->ImagePort.rastport );
  144.  
  145.       /* Draw shadow mask */
  146.       SetDrMd ( ibob->ShadowPort.rastport, JAM2 );
  147.       SetAPen ( ibob->ShadowPort.rastport, 1 );
  148.       SetBPen ( ibob->ShadowPort.rastport, 1 );
  149.  
  150.       Move ( ibob->ShadowPort.rastport, 0, YSHIFT );
  151.       Draw ( ibob->ShadowPort.rastport, size.x-1, YSHIFT );
  152.       Draw ( ibob->ShadowPort.rastport, size.x-1, y-1 );
  153.       Draw ( ibob->ShadowPort.rastport, 0, y-1 );
  154.       Draw ( ibob->ShadowPort.rastport, 0, YSHIFT-1 );
  155.  
  156.       Move ( ibob->ShadowPort.rastport, 1, YSHIFT+1 );
  157.       Draw ( ibob->ShadowPort.rastport, size.x-2, YSHIFT+1 );
  158.       Draw ( ibob->ShadowPort.rastport, size.x-2, y-2 );
  159.       Draw ( ibob->ShadowPort.rastport, 1, y-2 );
  160.       Draw ( ibob->ShadowPort.rastport, 1, YSHIFT+1 );
  161.  
  162.       length   = strlen(self->Label);
  163.       width    = length * FONT_WIDTH;
  164.       x        = size.x/2 - width/2;
  165.       y        = YSHIFT - FONT_HEIGHT/2 + BASELINE;
  166.  
  167.       Move( ibob->ShadowPort.rastport, x, y );
  168.       Text( ibob->ShadowPort.rastport, self->Label, length );
  169.  
  170.       SetLocation( self, location.x, location.y ); /* restore location. */
  171.       return TRUE;
  172.    }
  173.    return FALSE;
  174. }
  175.  
  176. struct BuilderMethods OutlineBox_bm;
  177. #endif
  178.  
  179. BOOL OutlineBox_elaborated = FALSE;
  180.  
  181. struct GraphicObjectClass OutlineBox_Class;
  182.  
  183.  
  184. void OutlineBoxClass_Init( struct GraphicObjectClass *class )
  185. {
  186.    GraphicObjectClass_Init( class );
  187.    class->isa         = GraphicObjectClass();
  188.    class->ClassName   = "OutlineBox";
  189.    class->CleanUp     = NULL;
  190.    class->Location    = OutlineBox_Location;
  191.    class->SetLocation = OutlineBox_SetLocation;
  192.    class->Size        = OutlineBox_Size;
  193.    class->AskSize     = OutlineBox_AskSize;
  194.    class->SetSize     = OutlineBox_SetSize;
  195.    class->SizeFlags   = GraphicObject_SizeFlagsAll;
  196.    class->Render      = OutlineBox_Render;
  197.    class->SetTitle    = OutlineBox_SetTitle;
  198.    class->Title       = OutlineBox_Title;
  199.  
  200. #ifdef BUILDER
  201.    go_InitBuilderMethods( &OutlineBox_bm );
  202.    OutlineBox_bm.New          = OutlineBox_New;
  203.    OutlineBox_bm.InitImageBob = OutlineBox_InitImageBob;
  204.    OutlineBox_bm.WriteCode    = OutlineBox_WriteCode;
  205.    class->BuilderMethods      = &OutlineBox_bm;
  206. #endif
  207.  
  208. }
  209.  
  210.  
  211. struct GraphicObjectClass *OutlineBoxClass( void )
  212. {
  213.    if (! OutlineBox_elaborated)
  214.    {
  215.       OutlineBoxClass_Init( &OutlineBox_Class );
  216.       OutlineBox_elaborated = TRUE;
  217.    }
  218.  
  219.    return &OutlineBox_Class;
  220. }
  221.  
  222. void OutlineBox_Init(  OutlineBox    *self,
  223.                        PIXELS       LeftEdge,
  224.                        PIXELS       TopEdge,
  225.                        PIXELS       Width,
  226.                        PIXELS       Height,
  227.                        pcg_3DPens   Pens,
  228.                        char        *Label )
  229. {
  230.    GraphicObject_Init(self);
  231.    
  232.    self->isa         = OutlineBoxClass();
  233.    self->PObjectName  = NULL;
  234.    self->Label       = NULL;
  235.    self->Pens        = Pens;
  236.    SetLocation( self, LeftEdge, TopEdge );
  237.    SetSize( self, Width, Height );
  238.    SetTitle( self, Label );
  239. }
  240.